<template>
  <view class="box">
    <!-- #ifdef MP-WEIXIN -->
    <view
      class="head"
      :style="
        'padding-top:' +
        statusBarHeight1 +
        'px;padding-right:' +
        (WXwidth + 15) +
        'px;height:' +
        (statusBarHeight1 + 44) +
        'px'
      "
    >
      <!-- #endif -->
      <!-- #ifndef MP-WEIXIN -->
      <view
        class="head"
        :style="
          'padding-top:' + (statusBarHeight1 + 10) + 'px;height:' + (statusBarHeight1 + 54) + 'px'
        "
      >
        <!-- #endif -->
        <image
          class="fanhui"
          @click="fanhui"
          src="https://pic.bangbangtongcheng.com/static/fanhui_bai.png"
          mode=""
        ></image>
        <view style="font-size: 36rpx; font-size: 500"> 添加收货地址 </view>
        <view class="fanhui"></view>
      </view>
      <!-- #ifdef MP-WEIXIN -->
      <view
        :style="'padding-top:' + statusBarHeight1 + 'px;height:' + (statusBarHeight1 + 44) + 'px'"
      ></view>
      <!-- #endif -->
      <!-- #ifndef MP-WEIXIN -->
      <view
        :style="'padding-top:' + statusBarHeight1 + 'px;height:' + (statusBarHeight1 + 54) + 'px'"
      ></view>
      <!-- #endif -->
      <view class="expressMail">
        <view class="information">
          <view class="item">
            <view class="title"> 收货人 </view>
            <input class="input" v-model="address.consignee" placeholder="请填写昵称" type="text" />
          </view>
          <view class="item">
            <view class="title"> 手机号码 </view>
            <input
              class="input"
              v-model="address.phoneNumber"
              placeholder="请填写手机号码"
              maxlength="11"
              type="number"
            />
          </view>
          <view class="item">
            <view class="title"> 所在地区 </view>
            <input
              class="input"
              v-model="address.region"
              style="padding-right: 54rpx"
              placeholder="请选择所在地区"
              type="text"
            />
            <image
              class="image"
              @click="openMap"
              src="https://pic.bangbangtongcheng.com/static/bbzc/dingwei.png"
              mode=""
            ></image>
          </view>
          <view class="detailAddress">
            <view class="title"> 详细地址 </view>
            <textarea
              class="input"
              v-model="address.detailedAddress"
              placeholder="请填写详细地址"
              cols="30"
              rows="10"
            ></textarea>
          </view>
        </view>
        <view class="define" @click="shippAddressData"> 确定 </view>
      </view>
      <uniBall></uniBall>
    </view>
  </view>
</template>
<script>
  import permision from '@/js_sdk/wa-permission/permission.js'
  //#ifdef H5
  import wxshare from '@/utils/index.js'
  //#endif
  import { mapGetters } from 'vuex'
  export default {
    data() {
      return {
        userInfo: uni.getStorageSync('Pduser'),
        WXwidth: '',
        latitude: '',
        longitude: '',
        address: {
          userId: '',
          consignee: '',
          phoneNumber: '',
          region: '',
          detailedAddress: '',
        },
      }
    },
    onLoad(options) {
      var than = this
      // #ifdef MP-WEIXIN
      // 获取胶囊信息
      let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
      let getWindowInfo = uni.getWindowInfo().screenWidth
      this.WXwidth = getWindowInfo - (menuButtonInfo.right - menuButtonInfo.width)
      // #endif
      if (uni.getStorageSync('locationState') == 1) {
        this.getLocation()
      }
      /* #ifdef H5 */
      this.jinzhi()
      /* #endif */
    },
    methods: {
      //保存地址
      shippAddressData() {
        let that = this
        this.address.userId = this.userInfo.id
        if (this.address.consignee == '') {
          uni.showToast({
            title: '请填写收货人姓名',
            icon: 'none',
            mask: true,
          })
          return false
        }
        if (this.address.phoneNumber == '') {
          uni.showToast({
            title: '请填写收货人手机号',
            icon: 'none',
            mask: true,
          })
          return false
        }
        if (this.address.region == '') {
          uni.showToast({
            title: '请填写收货人地址',
            icon: 'none',
            mask: true,
          })
          return false
        }
        this.$myRequest
          .post('/api/mobile/public/addOrModifyHarvestAddress', this.address)
          .then((res) => {
            uni.showToast({
              title: res.msg,
              icon: 'none',
              mask: true,
            })
            setTimeout(() => {
              that.fanhui()
            }, 500)
          })
      },
      jinzhi() {
        let _this = this
        let param = window.location.href.split('#')[0]
        uni.request({
          method: 'post',
          data: {
            url: param,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          url: 'https://www.bangbangtongcheng.com/conven//mob/login/getJsTicket.do',
          success(re) {
            _this.jinzhifx(re.data)
          },
        })
      },
      openMap() {
        let that = this
        if (that.latitude == '') {
          this.getLocation()
        }
        if (that.latitude == '') {
          return false
        }
        uni.chooseLocation({
          latitude: that.latitude,
          longitude: that.longitude,
          success: function (res) {
            that.address.region = res.address
          },
        })
      },
      jinzhifx(data) {
        wxshare.config({
          debug: false, //是否打开调试
          appId: data.appId, // 公众号的唯一标识
          timestamp: data.timestamp, // 生成签名的时间戳
          nonceStr: data.nonce, // ，生成签名的随机串
          signature: data.signature, // 签名
          jsApiList: ['hideMenuItems'],
        })
        // 禁止分享
        wxshare.hideMenuItems({
          // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
          menuList: [
            'menuItem:share:appMessage',
            'menuItem:share:timeline',
            'menuItem:share:timeline',
            'menuItem:share:qq',
            'menuItem:favorite',
            'menuItem:share:QZone',
            'menuItem:openWithSafari',
          ],
        })
      },
      async getLocation() {
        this.getLocation1()
      },
      getLocation1() {
        let _this = this
        uni.getLocation({
          type: 'gcj02',
          highAccuracyExpireTime: '3000',
          success(res) {
            console.log(res)
            _this.latitude = res.latitude
            _this.longitude = res.longitude
          },
          fail(res) {
            console.log(res, '经纬度没有授权')
            uni.setStorageSync('locationState', 0)
            if (uni.getStorageSync('locationState') == 0) {
              uni.showModal({
                title: '提示',
                content: '您已关闭位置权限或获取位置信息失败',
                success: function (res) {
                  if (res.confirm) {
                    // plus.runtime.openURL("app-settings:");
                    if (plus.os.name === 'iOS') {
                      plus.runtime.openURL('app-settings://')
                    } else {
                      var Intent = plus.android.importClass('android.content.Intent')
                      var Settings = plus.android.importClass('android.provider.Settings')
                      var Uri = plus.android.importClass('android.net.Uri')
                      var mainActivity = plus.android.runtimeMainActivity()
                      var intent = new Intent()
                      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                      var uri = Uri.fromParts('package', mainActivity.getPackageName(), null)
                      intent.setData(uri)
                      mainActivity.startActivity(intent)
                    }
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                },
              })
              return false
            }
          },
        })
      },
      fanhui() {
        uni.navigateBack({
          delta: 1,
        })
      },
    },
    computed: {
      ...mapGetters(['userid', 'islogin', 'statusBarHeight1', 'userinfo']),
    },
  }
</script>
<style lang="less" scoped>
  .box {
    width: 100%;
    min-height: 100vh;
    background-color: #fff;
  }

  .head {
    position: fixed;
    top: 0;
    left: 0;
    width: 750rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 15px;
    background-color: #64b6a8;
    z-index: 1000;
    color: #fff;

    .fanhui {
      width: 40rpx;
      height: 40rpx;
    }
  }
  .expressMail {
    margin-top: 20rpx;
    padding: 0 50rpx;
    .information {
      .item {
        display: flex;
        align-items: center;
        margin-bottom: 18rpx;
        position: relative;
        .title {
          flex: 1;
          font-size: 30rpx;
          font-weight: 400;
          color: #333333;
          margin-right: 10rpx;
        }
        .input {
          width: 520rpx;
          height: 60rpx;
          border: 1px solid #e0e0e0;
          padding: 0 10rpx;
          font-size: 24rpx;
        }
        .image {
          position: absolute;
          right: 10rpx;
          top: 50%;
          transform: translateY(-50%);
          width: 48rpx;
          height: 48rpx;
        }
      }
      .detailAddress {
        display: flex;
        .title {
          font-size: 30rpx;
          font-weight: 400;
          color: #333333;
          margin-right: 10rpx;
        }
        .input {
          width: 520rpx;
          height: 214rpx;
          border: 1px solid #e0e0e0;
          padding: 0 10rpx;
          font-size: 24rpx;
        }
      }
    }
    .define {
      position: fixed;
      bottom: 200rpx;
      left: 50%;
      transform: translateX(-50%);
      width: 670rpx;
      height: 80rpx;
      background-color: #64b6a8;
      border-radius: 50rpx;
      font-size: 34rpx;
      font-weight: 400;
      color: #ffffff;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
</style>
